package com.atguigu.gulimall.order.interceptor;

import com.atguigu.common.constant.AuthServiceConstant;
import com.atguigu.common.vo.MemberRespVo;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

@Component
public class OrderInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRespVo> threadLocal=new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

 /*       MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
                MediaType.TEXT_HTML,
                MediaType.TEXT_PLAIN));*/
 //       request.get
 //       restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);

        //库存查询订单状态放行 http://192.168.0.106:9060/order/order/status/202406132117167591801242435563704322
        String url= String.valueOf(request.getRequestURL());
        boolean match = new AntPathMatcher().match("http://192.168.0.106:9060/order/order/status/**", url);
        boolean match2 = new AntPathMatcher().match("http://9ff34313cc35.vicp.fun/testMol", url);
        if (match || match2){
            return true;
        }

        HttpSession session = request.getSession();
        MemberRespVo respVo=(MemberRespVo)session.getAttribute(AuthServiceConstant.LOGIN_REDIS_USER);
        if (respVo!=null){
            threadLocal.set(respVo);
        }else {
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://localhost:9090/login.html");
        }

        return true;
    }


}
